//
//  YPEmailCertificateViewController.m
//  ChengHuiTong
//
//  Created by 张无忌 on 16/4/11.
//  Copyright © 2016年 张文权. All rights reserved.
//

#import "YPEmailCertificateViewController.h"
#import "YPAgainEmailCertificateViewController.h"

@interface YPEmailCertificateViewController ()<UITextFieldDelegate>{
    
    
    UIView *_notEmailCertificateView;
    UIView *_emailCertificateView;
    
    UITextField * _emailTextField;
    
    UITextField * _certificateEmailTextField;
    UILabel * _certifiedStateContentLabel;
    UIButton *_rightButton;
    UIButton * _againCertificateButton;
}

@end

@implementation YPEmailCertificateViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"邮箱认证"];
}

- (void) viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"邮箱认证"];
}

- (void)viewDidLoad {
    [super viewDidLoad];


    self.navigationItem.title = @"邮箱认证";

    
    [self requestEmailCertificateData];
    
    
}



- (void)requestEmailCertificateData{
    
    
    
    YPEmailCertificateBin * bin = [[YPEmailCertificateBin alloc]init];
    bin.token=[YPShareAccountInfo shareAccountInfo].token;
    
    
    [YPHttpRequestMethod sendAsyRequestURL:CMD_CHECK_EMAIL_STATES withData:[bin dictionaryValue] withCompletionBlock:^(NSDictionary *responseDictionary) {
        
        NSLog(@"查询邮箱认证状态%@",responseDictionary);
        
        if ([responseDictionary[@"status"] intValue]==0) {
            
            self.emailCertificateModel = [[YPEmailCertificateModel alloc]initWithDictionary:responseDictionary];
            
            if ([self.emailCertificateModel.email_status intValue]==0) {
                
                
                [self CreatNotEmailCertificateView];

            }else{
                
                [self creatEmailCertificateView];
                
 
            }
            
            
            
            
        }
        
        
        
    } withFailedBlock:^(NSError *error) {
        
        
    } withIndicator:YES atView:self.view];
    
    
    
    
    
    
    
    
}

//初次认证邮箱的时候
- (void)CreatNotEmailCertificateView{
    
    
    _notEmailCertificateView = [[UIView alloc]initWithFrame:self.view.frame];
    _notEmailCertificateView.backgroundColor = UIColorFromRGB(0xf5f5f5);
    
    [self.view addSubview:_notEmailCertificateView];
    
    UILabel * warnLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    warnLabel.text = @"    提醒：一旦邮箱通过认证将不能修改。";
    warnLabel.backgroundColor = UIColorFromRGB(0xe0e0e0);
    warnLabel.textColor = UIColorFromRGB(0x929292);
    warnLabel.font = [UIFont systemFontOfSize:13];
    [_notEmailCertificateView addSubview:warnLabel];
    
    
    UIView * emailView = [[UIView alloc]initWithFrame:CGRectMake(0, warnLabel.bottom+10, SCREEN_WIDTH, 60)];
    emailView.backgroundColor = [UIColor whiteColor];
    [_notEmailCertificateView addSubview:emailView];
    
    
    UILabel * emailLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 40, 60)];
    //    emailLabel.backgroundColor = [UIColor orangeColor];
    emailLabel.text = @"邮箱";
//    emailLabel.font = [UIFont systemFontOfSize:16];
    emailLabel.textColor = UIColorFromRGB(0x555555);
    [emailView addSubview:emailLabel];
    
    
    _emailTextField = [[UITextField alloc]initWithFrame:CGRectMake(emailLabel.right+20, 0, SCREEN_WIDTH-15-40-20-15, 60)];
    _emailTextField.delegate = self;
    //    _emailTextField.backgroundColor = [UIColor cyanColor];
    _emailTextField.placeholder = @"请输入邮箱地址";
    
    _emailTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _emailTextField.returnKeyType = UIReturnKeyDone;
    [emailView addSubview:_emailTextField];
    
    
    
    UIButton * submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    submitButton.frame = CGRectMake(15, emailView.bottom+50, SCREEN_WIDTH-30, 44);
    submitButton.backgroundColor =  kColor_AppNavColor;
    [submitButton setTitle:@"确定提交" forState:0];
    [submitButton setTitleColor:[UIColor whiteColor] forState:0];
    submitButton.layer.cornerRadius =5;
    submitButton.clipsToBounds = YES;
    [submitButton addTarget:self action:@selector(submitButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_notEmailCertificateView addSubview:submitButton];
    
    
    
    
    
    
}
//认证邮箱之后
- (void)creatEmailCertificateView{
    
    _emailCertificateView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 80)];
    _emailCertificateView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_emailCertificateView];
    
    UILabel * certifiedEmailLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, 80, 20)];
//    certifiedEmailLabel.backgroundColor = [UIColor orangeColor];
    certifiedEmailLabel.text = @"认证邮箱";
    [_emailCertificateView addSubview:certifiedEmailLabel];
    
    _certificateEmailTextField = [[UITextField alloc]initWithFrame:CGRectMake(certifiedEmailLabel.right+10, 15, SCREEN_WIDTH-15-80-15-15-10-60, 20)];
    _certificateEmailTextField.userInteractionEnabled = NO;
    _certificateEmailTextField.font = [UIFont systemFontOfSize:14];
//    _certificateEmailTextField.backgroundColor = [UIColor yellowColor];
    [_emailCertificateView addSubview:_certificateEmailTextField];
    
    
    UIButton * reviseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    reviseButton.frame = CGRectMake(SCREEN_WIDTH-10-70, 10, 70, 25);
    [reviseButton setTitle:@"修改邮箱" forState:0];
    reviseButton.layer.borderColor =UIColorFromRGB(0x3592d3).CGColor;
    reviseButton.layer.borderWidth = 1;
    [reviseButton setTitleColor:UIColorFromRGB(0x3592d3) forState:0];
    reviseButton.layer.cornerRadius=5;
    reviseButton.clipsToBounds = YES;
    reviseButton.backgroundColor = [UIColor whiteColor];
    reviseButton.hidden = YES;
    reviseButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [reviseButton addTarget:self action:@selector(reviseButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_emailCertificateView addSubview:reviseButton];
    
    
    UILabel * certifiedStateLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, certifiedEmailLabel.bottom+10 , 80, 20)];
//    certifiedStateLabel.backgroundColor = [UIColor cyanColor];
    certifiedStateLabel.text = @"认证状态";
    [_emailCertificateView addSubview:certifiedStateLabel];
    
    _certifiedStateContentLabel = [[UILabel alloc]initWithFrame:CGRectMake(certifiedStateLabel.right+10, certifiedEmailLabel.bottom+10 , SCREEN_WIDTH-15-80-15-15-10-60, 20)];
//    _certifiedStateContentLabel.backgroundColor = [UIColor cyanColor];
    [_emailCertificateView addSubview:_certifiedStateContentLabel];
    _certificateEmailTextField.text =self.emailCertificateModel.email;
    

    _againCertificateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _againCertificateButton.frame = CGRectMake(SCREEN_WIDTH-10-70, certifiedEmailLabel.bottom+5, 70, 26);
    _againCertificateButton.layer.borderColor =UIColorFromRGB(0x3592d3).CGColor;
    _againCertificateButton.layer.borderWidth = 1;
    [_againCertificateButton setTitleColor:UIColorFromRGB(0x3592d3) forState:0];
    _againCertificateButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _againCertificateButton.layer.cornerRadius=5;
    _againCertificateButton.clipsToBounds = YES;
    [_againCertificateButton setTitle:@"重新认证" forState:0];
    [_againCertificateButton addTarget:self action:@selector(againCertificateButton) forControlEvents:UIControlEventTouchUpInside];
    _againCertificateButton.hidden = YES;
    [_emailCertificateView addSubview:_againCertificateButton];
    
    
//     self.emailCertificateModel.email_status = @"3";
    
    if ([self.emailCertificateModel.email_status intValue]==1){
        
        _certifiedStateContentLabel.text = @"正在核实中";
        _certifiedStateContentLabel.textColor = kColor_greyAshTitleColor;
        _certificateEmailTextField.textColor = kColor_greyAshTitleColor;

        reviseButton.hidden = NO;
        
        
    }else if ([self.emailCertificateModel.email_status intValue]==2){
        _certifiedStateContentLabel.text = @"已完成认证";
        _certifiedStateContentLabel.textColor = kColor_greyAshTitleColor;
        _certificateEmailTextField.textColor = kColor_greyAshTitleColor;

        
    }else if ([self.emailCertificateModel.email_status intValue]==3){
        
        _certifiedStateContentLabel.text = @"认证失败";
        _certifiedStateContentLabel.textColor = UIColorFromRGB(0xd91010);
        _certificateEmailTextField.textColor = kColor_greyAshTitleColor;

        _againCertificateButton.hidden = NO;

    }

    
}

#pragma mark -事件响应
- (void)againCertificateButton{
    
    
    YPAgainEmailCertificateViewController * againEmailCertificateVC = [[YPAgainEmailCertificateViewController alloc]init];
    [self.navigationController pushViewController:againEmailCertificateVC animated:YES];
}

- (void)rightButtonClick{
    
    YPEmailCertificateBin * bin = [[YPEmailCertificateBin alloc]init];
    bin.token=[YPShareAccountInfo shareAccountInfo].token;
    bin.email= _certificateEmailTextField.text;
    
    [YPHttpRequestMethod sendAsyRequestURL:CMD_EMAIL_VERIFY withData:[bin dictionaryValue] withCompletionBlock:^(NSDictionary *responseDictionary) {
        
        NSLog(@"发起邮箱认证请求%@",responseDictionary);
        
        if ([responseDictionary[@"status"] intValue]==0) {
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }else if ([responseDictionary[@"status"] intValue]==1202){
            
            [YRProgressHUD autoShowHUDAddedTo:self.view withText:@"token不匹配"];
            
        }else if ([responseDictionary[@"status"] intValue]==1402){
            
            [YRProgressHUD autoShowHUDAddedTo:self.view withText:@"邮箱地址无效"];

        }else if ([responseDictionary[@"status"] intValue]==1403){
            
            [YRProgressHUD autoShowHUDAddedTo:self.view withText:@"邮箱地址已被使用"];

            
        }else if ([responseDictionary[@"status"] intValue]==1404){
            
            [YRProgressHUD autoShowHUDAddedTo:self.view withText:@"发送邮件请求太频繁（限制60秒）"];

            
        }else if ([responseDictionary[@"status"] intValue]==9000){
            
            [YRProgressHUD autoShowHUDAddedTo:self.view withText:@"服务器繁忙"];

        }
        
        
        
    } withFailedBlock:^(NSError *error) {
        
        
    } withIndicator:YES atView:self.view];

    
}

- (void)reviseButtonClick{

    YPAgainEmailCertificateViewController * againEmailCertificateVC = [[YPAgainEmailCertificateViewController alloc]init];
    [self.navigationController pushViewController:againEmailCertificateVC animated:YES];


}
- (void)submitButtonClick{
    
    YPEmailCertificateBin * bin = [[YPEmailCertificateBin alloc]init];
    bin.token=[YPShareAccountInfo shareAccountInfo].token;
    bin.email= _emailTextField.text;
    
    
    [YPHttpRequestMethod sendAsyRequestURL:CMD_EMAIL_VERIFY withData:[bin dictionaryValue] withCompletionBlock:^(NSDictionary *responseDictionary) {
        
        NSLog(@"发起邮箱认证请求%@",responseDictionary);
        
        if ([responseDictionary[@"status"] intValue]==0) {
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }else if ([responseDictionary[@"status"] intValue]==1202){
            
            [YRProgressHUD autoShowHUDAddedTo:self.view withText:@"token不匹配"];
            
        }else if ([responseDictionary[@"status"] intValue]==1402){
            
            [YRProgressHUD autoShowHUDAddedTo:self.view withText:@"邮箱地址无效"];
            
        }else if ([responseDictionary[@"status"] intValue]==1403){
            
            [YRProgressHUD autoShowHUDAddedTo:self.view withText:@"邮箱地址已被使用"];
            
            
        }else if ([responseDictionary[@"status"] intValue]==1404){
            
            [YRProgressHUD autoShowHUDAddedTo:self.view withText:@"发送邮件请求太频繁（限制60秒）"];
            
            
        }else if ([responseDictionary[@"status"] intValue]==9000){
            
            [YRProgressHUD autoShowHUDAddedTo:self.view withText:@"服务器繁忙"];
            
        }

        
        
        
    } withFailedBlock:^(NSError *error) {
        
        
    } withIndicator:YES atView:self.view];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
